package com.kgc.scd.controller;


import com.kgc.scd.bean.*;
import com.kgc.scd.service.*;
import com.kgc.scd.util.PageSupport;
import com.kgc.scd.util.RedisUtils;
import com.kgc.scd.util.RequestResult;
import com.kgc.scd.util.ResultBuildUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * Created On : 2022/6/15.
 * <p>
 * Author     : hi
 * <p>
 * Description: 商品评论
 */
@Slf4j
@RestController
public class CommentController {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderItemService orderItemService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private UserService userService;

    @Autowired
    private SensitiveFeign sensitiveFeign;

  //  @Autowired
//    private SensitiveWordFilterUtil sensitiveWordFilterUtil;



    /**
     * @author : hi
     * @date   : 2022/6/15
     * @param  :
     * @return :
     * @description : 添加商品评论
     */
    @GetMapping("/addComment")
    public RequestResult<Object> addComment(@RequestParam String commProid ,
                                            @RequestParam String commContent,
                                            @RequestParam Integer commLevel,
                                            @RequestParam String orderId,
                                            @RequestParam String userId){
        RequestResult<String> stringRequestResult = sensitiveFeign.testSensitiveWord(commContent);
        String code = stringRequestResult.getCode();
        if(code.equals("500")){
            // 命中敏感词
            return ResultBuildUtil.fail("501","命中敏感词请重新输入");
        }
        //判断商品是否签收
        Order order = orderService.inquireOrder(orderId);
        if (order.getOrderState() != 3){
            return ResultBuildUtil.fail("502","该订单还未签收，无法评论");
        }

        //判断是否评论过
        OrderItem orderItem = orderItemService.inquireOrderItem(orderId);
        if (orderItem.getItemComment() == 0){
            //未评论过，开始评价
            Comment comment = new Comment();
            comment.setCommProid(commProid);
            comment.setCommContent(commContent);
            comment.setCommLevel(commLevel);
            comment.setCommDatetime(new Date());
            comment.setCommUserid(userId);
            commentService.addComment(comment);

            //修改orderitem中的评价状态
            OrderItem orderItem1 = orderItemService.inquireOrderItem(orderId);
            orderItem1.setItemComment(comment.getCommId());
            orderItem1.setItemId(orderItem1.getItemId());
            orderItemService.updateItem(orderItem1);
            log.info("----状态+:{}---",orderItem1);
            //增加积分
            User user1 = userService.selectUserCredit(userId);
            //查询积分
            user1.setUserCredit(user1.getUserCredit()+5);
            userService.updateCredit(user1);
            log.info("----积分+:{}---",user1);

            return ResultBuildUtil.success("评价成功");
        }
        return ResultBuildUtil.success("您已经评价过了，如需再次评价选择追评");
    }

    /**
     *
     * @return追加评论
     */
       @PostMapping("/superaddition")
       public RequestResult<Object> superaddition(@RequestParam String commProid ,
                                                  @RequestParam String orderId,
                                                  @RequestParam String commAdd,
                                                  @RequestParam String userId){
           RequestResult<String> stringRequestResult = sensitiveFeign.testSensitiveWord(commAdd);
           String code = stringRequestResult.getCode();
           if(code.equals("500")){
               // 命中敏感词
               return ResultBuildUtil.fail("501","命中敏感词请重新输入");
           }
           //判断是否追评过
           Comment comment = commentService.selectcomadd(commProid, orderId);
           String commAdd1 = comment.getCommAdd();
           if (commAdd1 != null && !commAdd1.equals("")  ){
               return ResultBuildUtil.fail("503","已经追评过，不可以在评价了");
           }
           //追评
           comment.setCommAdd(commAdd);
           comment.setCommProid(commProid);
           comment.setCommAddDatetime(new Date());
           comment.setCommUserid(userId);
           commentService.addsuperaddition(comment);

          return ResultBuildUtil.success("追评成功");
       }

       /**
        * @author : hi
        * @date   : 2022/6/16
        * @param  : [java.lang.String, java.lang.String]
        * @return : com.kgc.scd.util.RequestResult<java.lang.String>
        * @description : 查看我的待评价和已评价记录。
        */
       // 待评价
       @PostMapping("/FeedbackHistory")
       public RequestResult<PageSupport<OrderNotComment>> FeedbackHistory(@RequestParam String userId,
                                                                   @RequestParam("pageSize") Integer pageSize,
                                                                   @RequestParam("pageNo")Integer pageNo){
           // 查看待评价
           PageSupport<OrderNotComment> orderNotComments = orderItemService.showNoEvaluate(userId, pageSize, pageNo);
           log.info("我的待评价:{}",orderNotComments);
           return ResultBuildUtil.success(orderNotComments);
       }

       // 已评价
    @PostMapping("/OrderCommentList")
       public RequestResult<PageSupport<OrderComment>> OrderCommentList(@RequestParam("userId") String userId,
                                                                 @RequestParam("commLevel") Integer commLevel,
                                                                 @RequestParam("orderByDate") Integer orderByDate,
                                                                 @RequestParam("pageSize") Integer pageSize,
                                                                 @RequestParam("pageNo")Integer pageNo) {

        PageSupport<OrderComment> orderCommentPageSupport = orderItemService.showEvaluate(userId, commLevel, orderByDate, pageSize, pageNo);
        log.info("我的评价:{}", orderCommentPageSupport);
           //已评价记录
           return ResultBuildUtil.success(orderCommentPageSupport);
       }
}